package scala40rules;

import java.io.Serializable;
import java.util.ArrayList;

public class CardSetList implements Serializable{
	
	private static final long serialVersionUID = 1L;
	
	
	private ArrayList<CardSet> cardSetList = new ArrayList<CardSet>();

	public CardSetList(){
	}
	
	public CardSetList(ArrayList<CardSet> cardSetList){
		this.cardSetList = cardSetList;
	}
	
	public ArrayList<CardSet> getCardSetList() {
		return cardSetList;
	}

	public void setCardSetList(ArrayList<CardSet> cardSetList) {
		this.cardSetList = cardSetList;
	}
	
	public void setCardSetList(int index, CardSet cardSet){
		try{
//			if((index<this.cardSetList.size()) && (this.cardSetList.size()>0)){
//				this.cardSetList.remove(index);
				this.cardSetList.set(index, cardSet);
				
//		}
		}catch(Exception e){
			System.out.println("- Excepition- [CardSetList || setCardSetList()] Impossibile settare " +
					"il cardSet all'index: "+index+
							" this.cardSetList.size()="+this.cardSetList.size());
		}
	}
	
	public CardSetList removeAllCardSet(){
		CardSetList csl = new CardSetList();
		try{
			int i=0;
			while(i<this.cardSetList.size()){
				CardSet cardSet = this.cardSetList.remove(i);
				csl.addCardSet(cardSet);
			}
			System.out.println("[CardSetList || removeAllCardSet()]  CONTROLLO SE HO RIMOSSO TUTTI I CARDSET "+this.cardSetList.size());
			return csl;
		}catch(Exception e){
			System.out.println("- Excepition- [CardSetList || removeAllCardSet()] Impossibile rimuovere tutti i CardSet" +
							" this.cardSetList.size()="+this.cardSetList.size());
			return csl;
		}
	}
	
	public CardSet removeAllCardSetNotListed(){
		CardSet cardSet = new CardSet();
		try{
			int i=0;
			while(i<this.cardSetList.size()){
				cardSet.addCardSet(this.cardSetList.remove(i));
			}
			return cardSet;
		}catch(Exception e){
			System.out.println("- Excepition- [CardSetList || removeAllCardSetNotListed()] Impossibile rimuovere tutti i CardSet " +
							" this.cardSetList.size()="+this.cardSetList.size());
			return cardSet;
		}
	}

	public void addCardSetList(CardSetList csl){
		try{
			for(int i=0; i<csl.size(); i++){
				this.cardSetList.add(csl.getCardSet(i));
			}
		}catch(Exception e){
			System.out.println("- Excepition- [CardSetList || addCardSetList()] Impossibile aggiungere " +
					"il cardSetList "+
					" this.cardSetList.size()="+this.cardSetList.size());
		}
	}
	
	public void addCardSet(CardSet cardSet){
		try{
			this.cardSetList.add(cardSet);
		}catch(Exception e){
			System.out.println("- Excepition- [CardSetList || addCardSet()] Impossibile aggiungere " +
					"il cardSet; this.cardSetList.size()="+this.cardSetList.size());
		}
	}
	
	public void addCard(int index, Card card){
		try{
				this.cardSetList.get(index).addCard(card);
		
		}catch(Exception e){
			System.out.println("- Excepition- [CardSetList || setCardSetList()] Impossibile aggiungere " +
					"la carta "+ card.getSymbol()+" "+card.getSuit()+" all'index: "+index+
							" this.cardSetList.size()="+this.cardSetList.size());
		}
	}
	
	public CardSet removeCardSetFromList(int index){
		try{
			return this.cardSetList.remove(index);
			
		}catch(Exception e){
			System.out.println("- Excepition- [CardSetList || removeCardSetFromList()] Impossibile rimuovere " +
					"il cardSet dall'index: "+index+
					" this.cardSetList.size()="+this.cardSetList.size());
			return new CardSet();
		}
	}
	
	public CardSet getCardSet(int index){
		try{
			return this.cardSetList.get(index);
			
		}catch(Exception e){
			System.out.println("- Excepition- [CardSetList || getCardSet()] Impossibile ottenere " +
					"il cardSet all'index: "+index+
					" this.cardSetList.size()="+this.cardSetList.size());
			return new CardSet();
		}
	}
	
	public int size(){
		return this.cardSetList.size();
	}
	
	//TODO controllare se viene usato da qualche parte e modificarlo con due for annidati
	public boolean equalCardSetList(CardSetList csl){
		try{
			if( csl.size() == this.size()){
				for(int i=0; i<this.size(); i++){
					if ( !this.getCardSet(i).equalCardSet(csl.getCardSet(i)) ){
						return false;
					}
				}
			}else{
				return false;
			}
		}catch(Exception e){
			System.out.println("- Excepition- [CardSetList || equalCardSetList()] Impossibile controllare l'ugualianza dei due cardSetList " +
					"il size cardSet in input e': "+csl.size()+
							" mentre this.cardSetList.size()="+this.cardSetList.size());
			return false;
		}
		
		return true;
	}
	
}

	